<?php 
if(!isset($_SESSION['tkemail'])){
		if(!isset($_COOKIE['role'])){
			//$_SESSION['action']='entry';
			header( 'Location: index.php?action=entry' );
		}else{
			$_SESSION['tkemail']=$_COOKIE["tkemail"];
		    $_SESSION['role']=$_COOKIE["role"];
			
			$role=$_SESSION['role'];
			$tkemail=$_SESSION['tkemail'];
		}
	}else{
		$role=$_SESSION['role'];
		$tkemail=$_SESSION['tkemail'];
	}
	if($tkemail==""){
		header( 'Location: index.php?action=entry' );
	}

?>

<link rel="stylesheet" type="text/css" href="js/jsDatePick_ltr.min.css" />
<link
	rel="stylesheet" type="text/css" href="css/style-login.css" />
<script
	type="text/javascript" src="js/jsDatePick.min.1.3.js"></script>
<script
	type="text/javascript"
	src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"></script>
<link  href="css/jsDatePick_ltr.min.css" rel="stylesheet" type="text/css" media="all" />
<script type="text/javascript" src="css/jsDatePick.min.1.3.js"></script>
<script type="text/javascript">
window.onload = function(){
	new JsDatePick({
		useMode:2,
		target:"searchfrom",

		dateFormat:"%Y-%m-%d"
	});
	new JsDatePick({
		useMode:2,
		target:"searchto",

		dateFormat:"%Y-%m-%d"
	});
};
</script>
<?php include 'connection/connection.php';?>
<?php 

	$lang=$_SESSION['lang'];
	if($lang=='')
			$lang='EN';
	
	if ( !isset($_REQUEST['page'] ))
	{
    	$page = 1 ;
	}else{
		$page = $_REQUEST['page'] ;
	}
	
	$rows_per_page = 20;
	$page_start = ( $page - 1 ) * $rows_per_page; 
	$page_end = $page * $rows_per_page;
	
	$datecurrent = date("Y-m-d");
	$temp2days = strtotime($datecurrent.'-1 days');
    $day2 =  date("Y-m-d",$temp2days);
	
    $temp7days = strtotime($datecurrent.'-6 days');
    $day7 =  date("Y-m-d",$temp7days);
    
	$temp1month = strtotime($datecurrent.'-1 months');
	$onemonth = date("Y-m-d",$temp1month);
	
if (isset($_REQUEST['search'])){
	$searchfrom = $_REQUEST['searchfrom'];
	$searchto = $_REQUEST['searchto'];
	$search = $_REQUEST['search'];
	$selectlogserver = mysql_query("SELECT ls.*,ui.email FROM `user_info` ui, log_server ls WHERE ls.id = ui.id and ls.ins_time >'".$searchfrom." 00:00:00' AND ls.ins_time<'".$searchto." 23:59:59' ");
	$number_of_page = ceil ( mysql_num_rows( $selectlogserver ) / $rows_per_page ); 
	
}else{
	$searchfrom =date("Y-m-d");
	$searchto = date("Y-m-d");
	$selectlogserver = mysql_query("SELECT ls.*,ui.email FROM `user_info` ui, log_server ls WHERE ls.id = ui.id ");
	$number_of_page = ceil ( mysql_num_rows( $selectlogserver ) / $rows_per_page ); 
}


?>
<script type="text/javascript">
function ajaxserachLogServer(page,search){
	var searchfrom = $('input#searchfrom').val();
	var searchto = $('input#searchto').val();
	var status = document.getElementById('status').value; 
	$.ajax({url:"ajax_log/ajaxserachLogServer.php", data : {pages : page,search : search,searchfrom : searchfrom,searchto : searchto,status : status}, success:function(result){
		$("table#tbLogServer tr:not(:first)").remove();
		$("table#tbLogServer thead").append(result);
	}});
}
function paging(page,search,from,to){
	$.ajax({url:"ajax_log/ajaxPagingLogServer.php", data : {pages : page,search : search}, success:function(result){
		$("table#tbLogServer tr:not(:first)").remove();
		$("table#tbLogServer thead").append(result);
	}});
}
function ajaxsearchall(page,search){
	document.getElementById('searchfrom').value = "";
	document.getElementById('searchto').value = "";
	document.getElementById('status').value = '<?=l_all?>';
	$.ajax({url:"ajax_log/ajaxSearchAllLogServer.php", data : {pages : page,search : search}, success:function(result){
		$("table#tbLogServer tr:not(:first)").remove();
		$("table#tbLogServer thead").append(result);
	}});
}
function pagingsearch(page,search,from,to,status){
	$.ajax({url:"ajax_log/ajaxpagingsearchserver.php", data : {pages : page,search : search,searchfrom : from,searchto : to,status : status}, success:function(result){
		$("table#tbLogServer tr:not(:first)").remove();
		$("table#tbLogServer thead").append(result);
	}});
	
}
function ajaxSearchLogServerByOneDay(page,search,searchOneDay){
	$.ajax({url:"ajax_log/ajaxSearchLogServerByOneDay.php", data : {pages : page,search : search,searchOneDay : searchOneDay}, success:function(result){
		$("table#tbLogServer tr:not(:first)").remove();
		$("table#tbLogServer thead").append(result);
	}});
}
function pagingsearchByOneDay(page,search,searchOneDayfrom, searchOneDayTo){
	$.ajax({url:"ajax_log/ajaxpagingsearchServerByOneDay.php", data : {pages : page,search : search,searchOneDayfrom : searchOneDayfrom,searchOneDayTo : searchOneDayTo}, success:function(result){
		$("table#tbLogServer tr:not(:first)").remove();
		$("table#tbLogServer thead").append(result);
	}});
}

function ajaxSearchLogServerBy2Day(page,search,searchcurrent,searchlast2day){
	$.ajax({url:"ajax_log/ajaxSearchLogServerByTwoDay.php", data : {pages : page,search : search,searchcurrent : searchcurrent,searchlast2day : searchlast2day}, success:function(result){
		$("table#tbLogServer tr:not(:first)").remove();
		$("table#tbLogServer thead").append(result);
	}});
}

function pagingsearchByTwoDay(page,search,searchcurrent,searchlast2day){
	$.ajax({url:"ajax_log/ajaxpagingsearchServerByTwoDay.php", data : {pages : page,search : search,searchcurrent : searchcurrent,searchlast2day : searchlast2day}, success:function(result){
		$("table#tbLogServer tr:not(:first)").remove();
		$("table#tbLogServer thead").append(result);
	}});
}

function ajaxSearchLogServerBy7Days(page,search,searchcurrent,searchlast7day){
	$.ajax({url:"ajax_log/ajaxSearchLogServerBy7Day.php", data : {pages : page,search : search,searchcurrent : searchcurrent,searchlast7day : searchlast7day}, success:function(result){
		$("table#tbLogServer tr:not(:first)").remove();
		$("table#tbLogServer thead").append(result);
	}});
}

function pagingsearchBy7Day(page,search,searchcurrent,searchlast7days){
	$.ajax({url:"ajax_log/ajaxpagingsearchServerBy7Day.php", data : {pages : page,search : search,searchcurrent : searchcurrent,searchlast7days : searchlast7days}, success:function(result){
		$("table#tbLogServer tr:not(:first)").remove();
		$("table#tbLogServer thead").append(result);
	}});
}

function ajaxSearchLogServerBy1Months(page,search,searchcurrent,searchlast1month){
	$.ajax({url:"ajax_log/ajaxSearchLogServerBy1Month.php", data : {pages : page,search : search,searchcurrent : searchcurrent,searchlast1month : searchlast1month}, success:function(result){
		$("table#tbLogServer tr:not(:first)").remove();
		$("table#tbLogServer thead").append(result);
	}});
}

function pagingsearchBy1Month(page,search,searchcurrent,searchlast1month){
	$.ajax({url:"ajax_log/ajaxpagingsearchServerBy1Month.php", data : {pages : page,search : search,searchcurrent : searchcurrent,searchlast1month : searchlast1month}, success:function(result){
		$("table#tbLogServer tr:not(:first)").remove();
		$("table#tbLogServer thead").append(result);
	}});
}
</script>
<div class="maincon1">
	<div class="hbox">
		<h2>
		<?=$login?>
		</h2>
		<p>
		<?=$login1?>
		</p>
		<div class="seldown">
			<ul>
			<?php if ($role==0){?>
				<li class="hoso" style="float:left;">
		        	<a href="#" onclick="changePage('profile')" style="background:url(images/bg_aa.png) no-repeat; display:block; width:145px; height:71px"><span><?=$hoso?></span></a>
		        </li>
		        <li class="giayphep" style="float:left;">
		        	<a href="#" onclick="changePage('license')" style="background:url(images/bg_aa_normal.png) no-repeat; display:block; width:145px; height:71px"><span><?=$namelicense?></span></a>
		        </li>
			<?php }else if ($role==1){?>
				<li class="hoso" style="float:left;">
		        	<a href="#" onclick="changePage('profile')" style="background:url(images/bg_aa_normal.png) no-repeat; display:block; width:145px; height:71px"><span><?=$hoso?></span></a>
		        </li>
		        <li class="giayphep" style="float:left;">
		        	<a href="#" onclick="changePage('license')" style="background:url(images/bg_aa_normal.png) no-repeat; display:block; width:145px; height:71px"><span><?=$namelicense?></span></a>
		        </li>
		        <li class="server" style="float:left;">
		       	<a onclick="changePage('logserver')"  style="background:url(images/bg_aa.png) no-repeat; display:block; width:145px; height:71px;cursor: pointer;"><span class="choose" ><?=$logserver?></span></a>
		        </li>
		        <li class="viewer" style="float:left;">
		        	<a href="#"  onclick="changePage('logviewer')" style="background:url(images/bg_aa_normal.png) no-repeat; display:block; width:145px; height:71px"><span><?=$logviewer?></span></a>
		        </li>
		        <li class="viewertoserver" style="float:left;">
		        	<a href="#"  onclick="changePage('logviewertoserver')" style="background:url(images/bg_aa_normal.png) no-repeat; display:block; width:145px; height:71px"><span><?=$logviewertoserver?></span></a>
		        </li>
			<?php }?>
		    	
	  	  </ul>
		</div>
	</div>
</div>
<div style="text-align: center;padding-left: 40px;">
	<?=$l_from?> : <input type="text" id="searchfrom" name="searchfrom" style="height: 24px;width: 83px;">
	<?=$l_to?> : <input type="text" id="searchto" name="searchto" style="height: 24px;width: 83px;">
	<?=$namestatus?> : 
	<select id="status" name="status" style="height: 24px;width: 100px;padding-top: 3px;">
		<option value="all"><?=$l_all?></option>
		<option value="login"><?=$login?></option>
		<option value="logout"><?=$logout?></option>
	</select>
	<input id="search" name="search" value="<?=$l_search?>"
				  class="buttonNormal" type="button" onclick="ajaxserachLogServer(1,'<?=$search?>')"/>
	<input id="searchAll" name="searchAll" value="<?=$l_all?>"
				  class="buttonNormal" type="button" onclick="ajaxsearchall(1,'<?=$search?>')" />
	<input id="searchone" name="search" value="<?php echo $oneday?>"
				  class="buttonNormal" type="button" onclick="ajaxSearchLogServerByOneDay(1,'<?=$search?>','<?=$datecurrent?>')"/>
	<input id="search2day" name="searchAll" value="<?php echo $twoday?>"
				  class="buttonNormal" type="button" onclick="ajaxSearchLogServerBy2Day(1,'<?=$search?>','<?=$datecurrent?>','<?=$day2?>')"/>
	<input id="search1week" name="search" value="<?php echo $oneweek?>"
				  class="buttonNormal" type="button" onclick="ajaxSearchLogServerBy7Days(1,'<?=$search?>','<?=$datecurrent?>','<?=$day7?>')"/>
	<input id="search1month" name="searchAll" value="<?php echo $one_month?>"
				  class="buttonNormal" type="button" onclick="ajaxSearchLogServerBy1Months(1,'<?=$search?>','<?=$datecurrent?>','<?=$onemonth?>')"/>
</div>
<div class="maind" style="padding-top: 15px;">
	<div id="logserver" style="padding-left: 40px;">
		<table width="100%" cellpadding="0" cellspacing="0" class="mtable" id="tbLogServer">
			<thead>
				<tr>
					<td align="center" style="width: 4%"><?php echo $no?></td>
					<td align="center"><?php echo $useriforid?></td>
					<td align="center"><?php echo $remoteserverip ?></td>
					<td align="center"><?php echo $remoteserverhostname?></td>
					<td align="center"><?php echo $remoteserverver?></td>
					<td align="center"><?php echo $status?></td>
					<td align="center"><?php echo $remoteservertime?></td>
					<td align="center"><?php echo $inserttime?></td>
				</tr>
			</thead>
			<?php
			if(mysql_num_rows($selectlogserver)>0){
				$i=0;
				while ($row = mysql_fetch_array($selectlogserver)){
					if ( $i >= $page_start ){?>
				<tr>
						<td align="center" style="width: 4%"><?php echo $i+1?></td>
						<td align="center"><?php echo $row[email] ?></td>
						<td align="center"><?php echo $row[server_ip] ?></td>
						<td align="center"><?php echo $row[server_name]?></td>
						<td align="center"><?php echo $row[version]?></td>
						<td align="center"><?php echo $row[status]?></td>
						<td align="center"><?php echo $row[server_time]?></td>
						<td align="center"><?php echo $row[ins_time]?></td>
					</tr>
			<?php
					}
						$i++;
						if ($i >= $page_end){
							break;
						}
					}
				}
			?>
			
			<tr>
						<td colspan="8" class="rf-dt-shdr-c">
							<span style="padding:1px; display: inline-block;">
														
								<?php if ( $number_of_page > 1 ) 
								{ 
									if ($page==1)
										$p=2;
									else if ($page==$number_of_page)
										$p=$number_of_page-1;
									else $p=$page;
									if($page!=1){?>
										<a onclick="paging(<?=1?>,'<?=$search?>','<?=$searchfrom?>','<?=$searchto?>')" ><span class='button_next'><?php echo "<<<";?></span> </a>
										<a onclick="paging(<?=($page-1)?>,'<?=$search?>','<?=$searchfrom?>','<?=$searchto?>')"><span class='button_next'><?php echo "<<";?></span> </a>
									<?php  }
									else{
										echo "<span class='button_next'><<<</span> "; 
										echo "<span class='button_next'><<</span> ";
									}
									for ( $i = $p-1; $i <= $p+1; $i++ ) {	
											if ($i<=$number_of_page && $i>0)
											
												if ($i==$page){
													echo "<span class='button_next'><b>".$i."</b></span>"; 
												}else{
												?>
													<a onclick="paging(<?=$i?>,'<?=$search?>','<?=$searchfrom?>','<?=$searchto?>')"><span class="button_next">
													<?php 
													echo $i;
													?>
													</span> </a>
													<?php 
												}
											?>
										
									<?php 
											}
										
										if($page!=$number_of_page){ ?>
											<a onclick="paging(<?=($page+1)?>,'<?=$search?>','<?=$searchfrom?>','<?=$searchto?>')"><span class='button_next'><?php echo ">>";?></span> </a>
											<a onclick="paging(<?=($number_of_page)?>,'<?=$search?>','<?=$searchfrom?>','<?=$searchto?>')"><span class='button_next'><?php echo ">>>";?></span> </a>
										<?php }
										else{
											echo "<span class='button_next'>>></span> ";
											echo "<span class='button_next'>>>></span> ";
										}
									}
								
									
									?>
																						
							</span>
						</td>
					</tr>
		</table>
	</div>
</div>
<?php mysql_close($connection);?>